(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.companySetlinst.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'companySetlinstService' ];
	function updateVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, companySetlinstService) {
		var ctrl = this, companySetlinstInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.companySetlinst = {};
		
		ctrl.init = function() {
			var params = {
					taskId : companySetlinstInfo.taskId
			};
			// init cust prop define
			companySetlinstService.initProfSetlinst($scope, ctrl, true);
			ctrl.profSetlinstInnerGridOptions.enableRowHeaderSelection = false;
			//
			// init general
			companySetlinstService.initGeneral($scope, ctrl).then(function() {
				authService.updateVerify(params).then(function(response) {
					var initCompanySetlinst = response.data.model.newModel;
					companySetlinstService.resetSetlinstByData(initCompanySetlinst, true);
					ctrl.companySetlinst = initCompanySetlinst;
					ctrl.companySetlinst.taskId = response.data.task.taskId || companySetlinstInfo.taskId;
					//
					ctrl.companySetlinstOld = response.data.model.oldModel || {};
				});
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.companySetlinst.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.companySetlinst).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.companySetlinst.comments = comments;
						authService.updateVerifyReject(ctrl.companySetlinst).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.companysetlinst.list',
					'state' : 'cda.companySetlinst.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.companysetlinst.list',
					'state' : 'cda.companySetlinst.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);